package com.mayikt.manage;

import com.mayikt.base.BaseResponse;
import com.mayikt.constant.PayConstant;
import com.mayikt.feign.IntegralServiceFeign;
import com.mayikt.service.req.dto.IntegralReqDto;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@Component
@EnableAsync
@Slf4j
public class IntegralServiceManage {
    @Autowired
    private IntegralServiceFeign integralServiceFeign;

//    @Async
//    @GlobalTransactional
//    public void addIntegral(IntegralReqDto integralReqDto) {
//        try {
//            // 支付服务采用feign客户端调用积分服务接口
//            BaseResponse<String> stringBaseResponse = integralServiceFeign.addIntegral(integralReqDto);
//            Integer code = stringBaseResponse.getCode();
//            if (!(PayConstant.RPC_RESULT_OK_CODE.equals(code))) {
//                addErrorPayLog();
//                return;
//            }
//            // 增加积分成功了
//        } catch (Exception e) {
//            // 记录异常日志 后期采用定时任务实现补偿  积分服务接口宕机、或者超时
//            addErrorPayLog();
//        }
//        int i = 1 / 0;
//    }
//    private void addErrorPayLog(){}
//}

    @Async
//    @GlobalTransactional
    public void addIntegral(IntegralReqDto integralReqDto) {
        try {
            BaseResponse<String> stringBaseResponse = integralServiceFeign.addIntegral(integralReqDto);
            Integer code = stringBaseResponse.getCode();
            if (!(PayConstant.RPC_RESULT_OK_CODE.equals(code))) {
                // 日志记录 后期定时任务补偿
                addErrorPayLog();
                return;
            }
            // 增加积分成功
        } catch (Exception e) {
            addErrorPayLog();
            // 记录异常日志 后期采用定时任务实现补偿 积分服务接口宕机或者超时
        }
    }
    private void addErrorPayLog(){}
}
